package com.cjr.seataorder.controller;

import com.cjr.seataorder.dao.AddOrderMapper;
import com.cjr.seataorder.entity.Order;
import com.cjr.seataorder.feign.ProductFeign;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/order")
public class AddOrderController {
    @Resource
    AddOrderMapper addOrderMapper;
    @Autowired
    ProductFeign productFeign;

    @RequestMapping("/add/{orderId}")
    public String add(@PathVariable Integer orderId){
        addOrderMapper.addOrder(orderId);
        return "add ok";
    }

    @RequestMapping("/select")
    public String select(){
        List<Order> orders = addOrderMapper.selectOrder();
        Order order = orders.get(0);

        return order.toString();
    }

    @GlobalTransactional
    @RequestMapping("/op/{orderId}")
    public String op(@PathVariable Integer orderId){
        addOrderMapper.addOrder(orderId);
        productFeign.update();
        int i = 2/0;
        return "add and update ok";
    }
}
